package com.edu.cdp.service.impl;

import com.edu.cdp.VO.CategoryVO;
import com.edu.cdp.domain.Goods;
import com.edu.cdp.mapper.GoodsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.cdp.domain.Category;
import com.edu.cdp.service.CategoryService;
import com.edu.cdp.mapper.CategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Yang Can
* @description 针对表【category】的数据库操作Service实现
* @createDate 2024-06-02 19:38:05
*/
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
            GoodsMapper goodsMapper;

    /**
     * 修改商品分类
     * @param category
     * @return
     */
    @Override
    public Integer update(Category category) {
        Category oldCategory = categoryMapper.selectById(category.getId());

        if (oldCategory == null) {
            return 0;
        } else {
            int result = categoryMapper.updateById(category);

            if (result > 0){
                return 1;
            } else {
                log.error(category.getCategoryName()+ "修改失败");
                return -1;
            }
        }
    }

    /**
     * 获取商品分类名称
     * @param categoryName
     * @return
     */
    @Override
    public Category add(String categoryName) {
        QueryWrapper<Category> query = new QueryWrapper<>();

        query.eq("category_name", categoryName);

        Category oldCategory = categoryMapper.selectOne(query);

        if (oldCategory != null){
            log.error("{}已存在", categoryName);
            return null;
        } else {
            Category category = new Category();
            category.setCategoryName(categoryName);
            int insert = categoryMapper.insert(category);
            if (insert > 0){
                return category;
            } else {
                log.error(category.getCategoryName() + "添加失败");
                return null;
            }
        }
    }

    /**
     * 根据商品名称模糊查询
     * @param categoryName
     * @return
     */
    @Override
    public List<Category> fuzzQuery(String categoryName) {
        QueryWrapper<Category> query = new QueryWrapper<>();

        query.like("category_Name", categoryName);

        List<Category> categories = categoryMapper.selectList(query);

        if (categories != null) {
            return categories;
        } else {
            log.error(categoryName ,"{}查询失败");
            return null;
        }
    }

    /**
     * 删除商品，同时删除商品分类下所有商品
     * @param id
     * @return
     */
    @Override
    public void delete(Integer id) {
        int category = categoryMapper.deleteById(id);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("category_id", id);
        goodsMapper.delete(query);
    }

    @Override
    public CategoryVO getDetail(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            log.error("查无此分类");
            return null;
        }

        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.eq("category_id", id);

        List<Goods> goods = goodsMapper.selectList(query);

        CategoryVO categoryVO = new CategoryVO();
        categoryVO.setCategoryName(category.getCategoryName());
        categoryVO.setGoods(goods);

        return categoryVO;
    }
}




